import React, { Component } from 'react';
import { Modal, Form, Input, InputNumber, Select, message } from 'antd';
import fetchRequest from '../../fetch/fetchRequest';

const FormItem = Form.Item;
const { Option } = Select;

class CustomizedForm extends Component {
    state = {}
    componentWillMount() {
        fetchRequest.fetchR('users', 'GET')
            .then((response) => {
                this.setState({
                    userList: response
                })
            }).catch((err) => {  //网络请求失败返回的数据
                message.error('用户数据获取失败')
            });
    }
    // 单选框的选项
    getOptionList(data) {
        if (!data) {
            return [];
        }
        let options = [];
        data.forEach((item) => {
            options.push(<Option value={item.username} key={item.id}>{item.username}</Option>)
        })
        return options
    }

    render() {
        const { visible, formData, onCancel, onCreate, form, okText, title } = this.props;
        const { getFieldDecorator } = form;
        const FormItemLayout = {
            labelCol: { span: 5 },
            wrapperCol: { span: 16 },
        };
        return (
            <Modal
                visible={visible}
                title={title}
                okText={okText}
                cancelText='取消'
                onCancel={onCancel}
                onOk={onCreate}
            >
                <Form layout="horizontal">
                    <FormItem label="用户名" {...FormItemLayout} hasFeedback>
                        {getFieldDecorator('username', {
                            rules: [{ required: true, message: '请选择一个用户！' }],
                            initialValue: formData.username
                        })(
                            <Select
                                showSearch
                                style={{ width: 200 }}
                                placeholder="选择一个用户"
                                optionFilterProp="children"
                                filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                            >
                                {this.getOptionList(this.state.userList)}
                            </Select>
                        )}
                    </FormItem>
                    <FormItem label="本金" {...FormItemLayout} hasFeedback>
                        {getFieldDecorator('orderPrincipal', {
                            rules: [{ required: true, message: '请输入数值类型的本金！' }],
                            initialValue: formData.orderPrincipal
                        })(
                            <InputNumber />
                        )}
                    </FormItem>
                    <FormItem label="佣金" {...FormItemLayout} hasFeedback>
                        {getFieldDecorator('orderCommission', {
                            rules: [{ required: true, message: '请输入数值类型的佣金！' }],
                            initialValue: formData.orderCommission
                        })(
                            <InputNumber />
                        )}
                    </FormItem>
                    <FormItem label="备注" {...FormItemLayout} hasFeedback>
                        {getFieldDecorator('orderRemarks', {
                            initialValue: formData.orderRemarks
                        })(
                            <Input />
                        )}
                    </FormItem>
                </Form>
            </Modal>
        );
    }
}

const CollectionCreateForm = Form.create()(CustomizedForm);
export default CollectionCreateForm;